package cn.gemiman.daoyi.module.cms.controller.app.neteasecloudmusic;

import cn.gemiman.daoyi.framework.common.util.http.HttpUtils;
import cn.gemiman.daoyi.framework.common.util.servlet.ServletUtils;
import cn.gemiman.daoyi.framework.web.config.WebProperties;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "用户 APP - 云音乐")
@RestController
@RequestMapping("/cms/blog-music")
@Validated
@RequiredArgsConstructor
public class AppBlogMusicController {
    private final WebProperties webProperties;
    @Value("${daoyi.music.base-url}")
    private String musicBaseUrl;

    @GetMapping("/**")
    @Operation(summary = "音乐Api")
    public JSONObject get() {
        HttpServletRequest request = ServletUtils.getRequest();
        if (null == request) {
            return null;
        }
        String requestURI = request.getRequestURI().replace(webProperties.getAppApi().getPrefix(), "")
                .replace("/cms/blog-music", "");
        String url = HttpUtils.append(musicBaseUrl + requestURI, ServletUtils.getParamMap(request), null, false);
        String s = HttpUtil.get(url).replaceAll("http://", "https://");
        return JSONUtil.parseObj(s);
    }

}
